package com.jysz.certificate.web.action.pm;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.constant.OperCodeConstant;
import com.jysz.certificate.persistence.enums.PerformanceAssignStatus;
import com.jysz.certificate.persistence.model.pm.AssignUser;
import com.jysz.certificate.persistence.model.pm.UserPerformance;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.certificate.service.pm.UserPerformanceService;
import com.jysz.certificate.service.system.OrganizationService;
import com.jysz.certificate.utils.DateUtil;
import com.jysz.webcore.ssh.annotation.PermissionPoint;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/pm/userPm")
public class UserPerformanceAction extends AbstractBaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private UserPerformanceService userPerformanceService;
	@Resource
	private OrganizationService organizationService;
	
	private UserPerformance search;
	
	private UserPerformance entity;
	
	private String area;
	private String shopName;
	private Long[] id;
	
	@PermissionPoint(point = OperCodeConstant.M_PM)
	@Action(SHOW)
	public String show(){
		entity = userPerformanceService.get(entity.getId());
		return SHOW;
	}
	
	@PermissionPoint(point = OperCodeConstant.M_PM)
	@Action(INDEX)
	public String index(){
		this.setPage(userPerformanceService.findPage(search, this.getCurrentPage(), this.getPageSize(), this.getUser(), area, shopName));
		
		return INDEX;
	}
	
	@PermissionPoint(point = OperCodeConstant.PM_PAYMENT_CONFIRM)
	@Action("paymentConfirmAll")
	public String paymentConfirmAll(){
		for (int i = 0; i < id.length; i++) {
			entity = userPerformanceService.find(id[i]);
			entity.setStatus(PerformanceAssignStatus.PAYMENT);
			entity.setRealAmount(entity.getAmount());
			entity.setPaymentDate(DateUtil.getDate());
			entity.setPaymentUser(this.getUser());
			
			userPerformanceService.save(entity);
		}
		setRedirectUrl("index.do?currentPage=" + getCurrentPage());
		this.setSuccessPopMessage("批量核发成功。。");
		return SHOW_MSG_PAGE;
	}
	
	@PermissionPoint(point = OperCodeConstant.PM_PAYMENT_CONFIRM)
	@Action("paymentConfirm")
	public String paymentConfirm(){
		entity = userPerformanceService.find(entity.getId());
		entity.setStatus(PerformanceAssignStatus.PAYMENT);
		entity.setRealAmount(entity.getAmount());
		entity.setPaymentDate(DateUtil.getDate());
		entity.setPaymentUser(this.getUser());
		userPerformanceService.save(entity);
		
		setRedirectUrl("index.do?currentPage=" + getCurrentPage());
		this.setSuccessPopMessage("该人员绩效已经核发。。");
		return SHOW_MSG_PAGE;
	}
	

	/**
	 * @return
	 */
	public String getNotReShopNames(UserPerformance userPerformance){
		
//		User user = userPerformance.getUser();
		StringBuilder shopNames = new StringBuilder("");
//		if(user != null){
			List<AssignUser> assignUsers = userPerformance.getAssignUsers();
			Set<Long> shops = new HashSet<Long>();
			for (Iterator<AssignUser> iterator = assignUsers.iterator(); iterator.hasNext();) {
				AssignUser assignUser =  iterator.next();
				Shop shop = assignUser.getPaShopMonthDetail().getPerformanceAssignShopDetail().getShop();
				if(!shops.contains(shop.getId())){
					shopNames.append("(");
					shopNames.append(shop.getShopMark());
					shopNames.append(")");
					shopNames.append(shop.getShopName());
					shopNames.append(",");
					shops.add(shop.getId());
				}
			}
//		}
//		else{
//			 List<PAShopMonthDetail> details = userPerformance.getPaShopMonthDetails();
//			 Set<Long> shops = new HashSet<Long>();
//			 
//			for (Iterator<PAShopMonthDetail> iterator = details.iterator(); iterator.hasNext();) {
//				PAShopMonthDetail paShopMonthDetail = (PAShopMonthDetail) iterator
//						.next();
//				
//				Shop shop = paShopMonthDetail.getPerformanceAssignShopDetail().getShop();
//				
//				if(!shops.contains(shop.getId())){
//					shopNames.append("(");
//					shopNames.append(shop.getShopMark());
//					shopNames.append(")");
//					shopNames.append(shop.getShopName());
//					shopNames.append(",");
//					shops.add(shop.getId());
//				}
//			}
//		}
		
		return shopNames.length() >= 18 ?  shopNames.substring(0, 17) + "..." :shopNames.substring(0, shopNames.length() -1);
	}
	
	public String getNotReAllShopNames(UserPerformance userPerformance){
		
//		User user = userPerformance.getUser();
		StringBuilder shopNames = new StringBuilder("");
//		if(user != null){
			List<AssignUser> assignUsers = userPerformance.getAssignUsers();
			Set<Long> shops = new HashSet<Long>();
			for (Iterator<AssignUser> iterator = assignUsers.iterator(); iterator.hasNext();) {
				AssignUser assignUser =  iterator.next();
				Shop shop = assignUser.getPaShopMonthDetail().getPerformanceAssignShopDetail().getShop();
				if(!shops.contains(shop.getId())){
					shopNames.append("(");
					shopNames.append(shop.getShopMark());
					shopNames.append(")");
					shopNames.append(shop.getShopName());
					shopNames.append(",");
					shops.add(shop.getId());
				}
			}
		return shopNames.toString().substring(0, shopNames.toString().length() -1);
	}
	
	/**
	 * @return
	 */
	public String getNotReAreaNames(UserPerformance userPerformance){
		
		StringBuilder areaNames = new StringBuilder("");
		
		if(userPerformance.getAssignUsers() != null && userPerformance.getAssignUsers().size() > 0){
			List<AssignUser> assignUsers = userPerformance.getAssignUsers();
			Set<Long> orgs = new HashSet<Long>();
			for (Iterator<AssignUser> iterator = assignUsers.iterator(); iterator.hasNext();) {
				AssignUser assignUser =  iterator.next();
				Organization org = assignUser.getPaShopMonthDetail().getPerformanceAssignShopDetail().getShop().getArea();
				if(!orgs.contains(org.getId())){
					org = organizationService.get(org.getParent().getId());
					areaNames.append(areaNames.indexOf(org.getName())>-1?"":org.getName()+",");
					orgs.add(org.getId());
				}
			}
		}
//		else{
//			 List<PAShopMonthDetail> details = userPerformance.getPaShopMonthDetails();
//			 Set<Long> orgs = new HashSet<Long>();
//			 
//			for (Iterator<PAShopMonthDetail> iterator = details.iterator(); iterator.hasNext();) {
//				PAShopMonthDetail paShopMonthDetail = (PAShopMonthDetail) iterator
//						.next();
//				
//				Organization org = paShopMonthDetail.getPerformanceAssignShopDetail().getShop().getArea();
//				
//				if(!orgs.contains(org.getId())){
//					areaNames.append(org.getName());
//					areaNames.append(",");
//					orgs.add(org.getId());
//				}
//			}
//		}
		
		return areaNames.substring(0, areaNames.length() -1);
	}
	
	
	public UserPerformance getSearch() {
		return search;
	}

	public void setSearch(UserPerformance search) {
		this.search = search;
	}

	public UserPerformance getEntity() {
		return entity;
	}

	public void setEntity(UserPerformance entity) {
		this.entity = entity;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}
	public Long[] getId() {
		return id;
	}
	public void setId(Long[] id) {
		this.id = id;
	}
	
}
